package com.cr.admin.business.vehicle.dao.repository;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cr.admin.business.topic.pojo.entity.Topic;
import com.cr.admin.business.vehicle.dao.mapper.VehicleMapper;
import com.cr.admin.business.vehicle.pojo.entity.Vehicle;
import com.cr.admin.business.vehicle.pojo.param.VehicleAddParam;
import com.cr.admin.business.vehicle.pojo.param.VehicleUpdateParam;
import com.cr.admin.business.vehicle.pojo.query.Query;
import com.cr.admin.business.vehicle.pojo.query.VehicleQuery;
import com.cr.admin.business.vehicle.pojo.vo.VehicleVO;
import com.cr.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

@Repository
public class VehicleRepositoryImpl implements VehicleRepository{
    @Autowired
    private VehicleMapper vehicleMapper;


    @Override
    public List<Vehicle> selectList(VehicleQuery vehicleQuery) {
        QueryWrapper<Vehicle> queryWrapper= new QueryWrapper<>();
        if(vehicleQuery.getBrandInfo()!=null){
            queryWrapper.like("brand_info",vehicleQuery.getBrandInfo());
        }
        if(vehicleQuery.getCityId()!=null){
            queryWrapper.like("city_id",vehicleQuery.getCityId());
        }
        if(vehicleQuery.getPlateNumber()!=null){
            queryWrapper.like("plate_number",vehicleQuery.getPlateNumber());
        }
        return vehicleMapper.selectList(queryWrapper);
    }

    @Override
    public void insert(VehicleAddParam vehicleAddParam) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicleMapper.insert(vehicle);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ServiceException("参数不能为空");
        }
        vehicleMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(VehicleUpdateParam vehicleUpdateParam) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleUpdateParam,vehicle);
        vehicleMapper.updateById(vehicle);
    }

    @Override
    public List<Vehicle> selectList(Query query) {
       return vehicleMapper.selectList(query);
    }

    @Override
    public VehicleVO selectById(Long id) {

          Vehicle vehicle  =   vehicleMapper.selectById(id);
          if (vehicle != null){
              VehicleVO vehicleVO = new VehicleVO();
              BeanUtils.copyProperties(vehicle,vehicleVO);
              return vehicleVO;
          }
          return null;
    }


}
